home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / AMarquee / examples / StreamCheck.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  3KB  |  115 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13.  
  14. #define UNLESS(x) if (!(x))
  15.  
  16. struct Library * AMarqueeBase = NULL;
  17. struct QSession * session     = NULL;
  18.  
  19. void CleanExit(void)
  20. {
  21.   if (session)      QFreeSession(session);       /* This MUST be done before we close the library! */
  22.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);
  23.   printf("All done.\n");
  24. }
  25.  
  26. /* Main program--simply increments our counter using a stream. */
  27. int main(int argc, char ** argv)
  28. {
  29.   char * connectTo, * progName;
  30.   int port = 2957;
  31.   int count = 0;
  32.   
  33.   atexit(CleanExit);
  34.     
  35.   printf("StreamCheck: This program is designed to be used with StreamGen.\n");
  36.   printf("             It will watch StreamGen's output and note any streaming\n");
  37.   printf("             errors (skipped or duplicated numbers).\n");
  38.   printf("             Note that only one StreamGen should be running!\n");
  39.   
  40.   printf("Usage:  StreamCheck [hostname=localhost] [name=streamcheck]\n");
  41.   connectTo = argv[1] ? argv[1] : "localhost";
  42.   progName  = argv[2] ? argv[2] : "streamcheck";
  43.  
  44.   if ((AMarqueeBase = OpenLibrary("amarquee.library",37L)) == NULL)
  45.   {
  46.     printf("Couldn't open amarquee.library v37!\n");
  47.     exit(RETURN_ERROR);
  48.   }
  49.   printf("Connecting to %s:%i...\n",connectTo, port);
  50.   if ((session = QNewSession(connectTo, port, progName)) == NULL)
  51.   {
  52.     printf("Couldn't connect to server %s:%i\n",connectTo, port);
  53.     CloseLibrary(AMarqueeBase);
  54.     exit(RETURN_WARN);
  55.   }
  56.   printf("StreamCheck connected to server %s:%i\n",connectTo, port);
  57.  
  58.   UNLESS((QSubscribeOp(session, "/#?/#?/streamcount", sizeof(count))) &&
  59.          (QGo(session,0L)))
  60.   {
  61.     printf("Setup error.\n");
  62.     exit(RETURN_ERROR);
  63.   }
  64.   
  65.   /* Setup */
  66.   while(1)
  67.   {
  68.     ULONG signals = Wait(SIGBREAKF_CTRL_C | (1L<<session->qMsgPort->mp_SigBit));
  69.     
  70.     if (signals & SIGBREAKF_CTRL_C)
  71.     {
  72.       printf("Aborting...\n");
  73.       break;
  74.     }
  75.     if (signals & (1L<<session->qMsgPort->mp_SigBit))
  76.     {
  77.       struct QMessage * qmsg;
  78.       BOOL BDie = FALSE;
  79.       
  80.       while((BDie == FALSE)&&(qmsg = GetMsg(session->qMsgPort)))
  81.       {
  82.         if (qmsg->qm_Status != QERROR_NO_ERROR)
  83.         {
  84.           printf("Received an error, aborting!\n");
  85.           BDie = TRUE;
  86.         }
  87.         else
  88.         {
  89.           if (qmsg->qm_Data) 
  90.           {
  91.             if (count != 0)
  92.             {
  93.               int newVal = *((int *)qmsg->qm_Data);
  94.               
  95.               if (newVal != count+1) printf("Stream Error:  count was %i, newVal was %i.\n",count, newVal);
  96.               count = newVal;
  97.               printf("Message %p: count = %i\r", qmsg, count); fflush(stdout);
  98.             }
  99.             else count = *((int *)qmsg->qm_Data);
  100.           }
  101.           else
  102.           {
  103.             printf("StreamGenerator terminated, exiting!\n");
  104.             BDie = TRUE;
  105.           }
  106.         }
  107.         FreeQMessage(session, qmsg);
  108.       }
  109.       
  110.       if (BDie) break;
  111.     }
  112.   }
  113.   /* CleanExit called here via the atexit() feature */
  114. }
  115.